require "base/internal/ui/reflexcore"

MgHUD_WeaponRack = 
{
    userData = {}
}
registerWidget("MgHUD_WeaponRack");


function MgHUD_WeaponRack:draw()

    ------ Early out if HUD shouldn't be shown. ------
    if not shouldShowHUD() then return end
	if isRaceMode() then return end

    ------ GET PLAYER ------
    local player = getPlayer();

    ------ TEXT FORMATTING OPTIONS -------
    nvgFontSize(130);
    nvgFontFace("CodeProLC");
    nvgTextAlign(NVG_ALIGN_LEFT, NVG_ALIGN_TOP);

    ------ CODE GOES HERE ------
    

    local weaponCount = 8; -- table.maxn(player.ammo);
    local spaceCount = weaponCount - 1;
    
    ------ OPTIONS ------
    local weaponWidth = 60;
    local weaponHeight = weaponWidth;
    local weaponSpacing = 30; -- 0 or -1 to remove spacing
    
    ------ HELPERS ------
    local rackWidth = (weaponWidth * weaponCount) + (weaponSpacing * spaceCount);
    local rackLeft = -(rackWidth / 2);
    local weaponX = rackLeft-weaponWidth/2;
    local weaponY = 0;

    for weaponIndex = 1, weaponCount do

        local weapon = player.weapons[weaponIndex];
        local color = weapon.color

        -- if weapon not picked up, color it gray
        if not weapon.pickedup then
            color = Color(128,128,128,color.a)
        end

        -- selected weapon glow
        local glowX = weaponX + weaponHeight*0.5
        local glowY = weaponY + weaponHeight*0.5
        local radius = weaponHeight * 3/4

		if weaponIndex == player.weaponIndexSelected then
			nvgBeginPath();
			nvgCircle(glowX, glowY, radius)
			nvgFillRadialGradient(glowX, glowY, 15, radius, color, Color(0,0,0,0));
			nvgFill();
		end

        -- icon
        local iconRadius = weaponHeight * 0.5;
        local iconX = weaponX + (weaponHeight - iconRadius);
        local iconY = weaponHeight / 2
        local iconColor = color;

        if weaponIndex == player.weaponIndexSelected then
            iconColor = Color(255,255,255,255)
        end

        -- melee svg
        local svgName = "internal/ui/icons/weapon"..weaponIndex;
        if (weaponIndex == 1) and (player.inventoryMelee ~= nil) then
            local def = inventoryDefinitions[player.inventoryMelee];
            if def ~= nil then
                svgName = def.asset;
            end
        end

        -- drop shadow
        nvgFillColor(Color(48,48,48));
        nvgSvg(svgName, iconX+1.85, iconY+0.92, iconRadius);
        -- colored icon
        nvgFillColor(iconColor);
        nvgSvg(svgName, iconX, iconY, iconRadius);

        -- ammo
        local ammoX = weaponX + (iconRadius);
        local ammoY = weaponY + weaponHeight*.60
        local ammoCount = player.weapons[weaponIndex].ammo;

        if weaponIndex == 1 then ammoCount = "" end
        if weaponIndex == player.weaponIndexSelected then ammoCount = "" end
        if not weapon.pickedup then ammoCount = "" end

        nvgFontSize(weaponHeight);
        nvgFontFace("CodeProLC");
        nvgTextAlign(NVG_ALIGN_CENTER, NVG_ALIGN_MIDDLE)
        nvgFontBlur(0)

        nvgFillColor(Color(48,48,48))
        nvgText(ammoX+weaponHeight*(2/130), ammoY+2*weaponHeight*(2/130), ammoCount);

        nvgFillColor(Color(255,255,255,255))
        nvgText(ammoX, ammoY, ammoCount);


        weaponX = weaponX + weaponWidth + weaponSpacing;

    end


end
